package com.hss.questions.arr;

import com.hss.sort.Print;

/**
 http://www.cyc2018.xyz/%E7%AE%97%E6%B3%95/%E5%89%91%E6%8C%87%20Offer%20%E9%A2%98%E8%A7%A3/3.%20%E6%95%B0%E7%BB%84%E4%B8%AD%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0%E5%AD%97.html#%E9%A2%98%E7%9B%AE%E6%8F%8F%E8%BF%B0

 在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的，但不知道有几个数字是重复的，也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
 * */
public class SearchMutiNumber {

    public static void main(String[] args) {
        int[] arr = {2, 3, 1, 0, 2, 5};
        int[]duplication = new int[arr.length];
        SearchMutiNumber searchMutiNumber = new SearchMutiNumber();
        System.out.println("value = " + searchMutiNumber.search(arr, arr.length, duplication));
        System.out.printf("xx = "+duplication[0]);
    }

    boolean search (int[] arr, int length, int[]duplication){
            for (int i=0;i<length;i++){
                while (arr[i] != i){
                   if (arr[i] == arr[arr[i]]){
                       duplication[0] = arr[i];
                       return true;
                   }
                    swap(arr,i,arr[i]);
                    Print.printArr(arr);
                }

            }
            return false;
    }




    private void swap(int[] nums, int i, int j) {
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
}
